import 'package:library_base/entities/stock_out_order_goods_detail_entity.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/net/apis.dart';
import 'package:library_base/provider/base_controller.dart';
import 'package:library_base/utils/toast_util.dart';
import 'package:get/get.dart';
import 'package:library_base/ext/double_ext.dart';


class BaseInventoryDistributionSingleController extends BaseController {

  late String stockOutId;

  late String goodsCode;

  late String goodsName;

  late List<StockOutOrderGoodsDetailEntity> checkedStockOutOrderGoodsDetailEntity;

  late List<StockOutOrderGoodsDetailEntity> goodsDetails;

  late List<bool> isSelectedData = [];

  ///是否可分配商品,如果是已完成的商品不可再进行分配
  bool enablePreDistributedGoods = true;

  @override
  void onCreate() {
    super.onCreate();
    stockOutId = getArguments()['stockOutId'];
    goodsCode = getArguments()['goodsCode'];
    goodsDetails = getArguments()['goodsDetails'];
    enablePreDistributedGoods = getArguments()['enablePreDistributedGoods'];
    checkedStockOutOrderGoodsDetailEntity = [];
    for (int i = 0; i < goodsDetails.length; i++) {
      if((goodsDetails[i].stockOutCount??0.0)>0){
        isSelectedData.add(true);
        checkedStockOutOrderGoodsDetailEntity.add(goodsDetails[i]);
      }else{
        isSelectedData.add(false);
      }
    }
  }

  void distribute(){
    if(checkedStockOutOrderGoodsDetailEntity.isEmpty){
      ToastUtil.normal(MultiLanguage.pleaseSelectItemsInStock.tr);
      return;
    }

    var map = {};
    map['stockOutId'] = stockOutId;
    map['goodsCode'] = goodsCode;

    var goodsDetails = [];
    for(int i = 0;i<checkedStockOutOrderGoodsDetailEntity.length;i++){
      var selectData = checkedStockOutOrderGoodsDetailEntity[i];
      var detail = {};
      detail['goodsBarCode'] = selectData.goodsBarCode;
      detail['batchNo'] = selectData.batchNo;
      detail['locationId'] = selectData.locationId;
      detail['stockOutCount'] = selectData.stockOutCount;
      detail['stockOutWeight'] = selectData.stockOutWeight.numAsUnitSubmit();
      goodsDetails.add(detail);
    }
    map['goodsDetails'] = goodsDetails;
    requestUniversal<bool>(Apis.STOCK_OUT_LOCK_GOODS,
        params: map, resultSuccessCall: (result) async {
            Get.back();
        });
  }


}
